import {
  com_CancellikingsApi,
  com_likingssApi,
  SendCommentParamsType,
  SendCommentsByIdAPI,
} from './../../api/comment';
import { CommentActionType, CommentType } from './../reducer/comment';
import { AppDispatch } from './../index';
import { getCommentsByIdAPI, CommentParamsType } from '@/api/comment';

/** 获取评论  action*/
export const getCommentsByIdAction = (params: CommentParamsType) => {
  return async (dispatch: AppDispatch) => {
    const res = await getCommentsByIdAPI(params);
    dispatch({ type: 'comment/SaveCommentInfo', payload: res.data });
  };
};

/** 清空评论  action*/
export const ClearCommentAction = () => {
  return { type: 'comment/ClearCommentInfo' };
};

/** 保存原始评论  action*/
export const SaveOriginCommentAction = (
  comment: CommentType
): CommentActionType => {
  return { type: 'comment/SaveOriginComment', payload: comment };
};

/** 清空原始评论 action*/
export const ClearOriginCommentAction = (): CommentActionType => {
  return { type: 'comment/ClearOriginComment' };
};

/** 对文章或者评论进行评论  action*/
export const SendCommentsByIdAction = (params: SendCommentParamsType) => {
  return async () => {
    await SendCommentsByIdAPI(params);
  };
};

/** 根据id获取评论的回复  action*/
export const GetreplyByIdAction = (params: CommentParamsType) => {
  return async (dispatch: AppDispatch) => {
    const res = await getCommentsByIdAPI(params);
    dispatch({ type: 'comment/SaveReplys', payload: res.data });
  };
};

/** 清空评论  action*/
export const ClearReplysAction = () => {
  return { type: 'comment/ClearReplys' };
};

/** 点赞文章评论回复 action*/
export function Com_likingsAction(com_id: string, attitude: number) {
  return async (dispatch: AppDispatch) => {
    if (attitude === 1) {
      await com_CancellikingsApi(com_id);
    } else {
      await com_likingssApi(com_id);
    }

    // dispatch({
    //   type: 'comment/originComment',
    //   payload: attitude === 1 ? -1 : 1,
    // });
  };
}
